﻿using Models.OrderMealManageSubSystem;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class OrderParameterSettingDAL
    {
        private static string conn = ConfigurationSettings.AppSettings["con"].ToString();

        public static DataSet GetOrderParameterSetting(string openid)
        {
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@openid",openid)
            };
            string sqlString = $@"
                            select 
                                breakfastAmount as breakfastAmount,
                                lunchAmount as lunchAmount,
                                dinnerAmount as dinnerAmount,
                                orderSwitch as orderSwitch
                            from MenuType

                            select
                                Convert(nvarchar(20), orderDate, 23) as orderDate
                            from orderDate  order by orderDate asc

                            select rfkh,ryxm,rybh,klb,rfye,grye,btye,yxq,bmmc from ryxx where ryid in (select ryid From IdBind where openid=@openid) 


                            select count(1) as thisMonthCount From OrderList o with(nolock) inner join OrderDetail l with(nolock) on o.OrderId=l.OrderId
                            where 1=1 and Ryid in (select ryid from idbind where openid=@openid)
                            and l.reservationdate between DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) and  CONVERT(DATETIME, CONCAT(EOMONTH(GETDATE()), ' 23:59:59'))


                            select count(1) as nextMonthCount From OrderList o with(nolock) inner join OrderDetail l with(nolock) on o.OrderId=l.OrderId
                            where 1=1 and Ryid in (select ryid from idbind where openid=@openid) 
                            and l.reservationdate between DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())+1, 0) and  DATEADD(MONTH, 1, CONCAT(EOMONTH(GETDATE()), ' 23:59:59'))


                        ";
            try
            {
                return SqlHelper.ExecuteDataSet(conn, CommandType.Text, sqlString, sp);
            }
            catch (Exception ex)
            {
                ApplicationLog.Write("错误输出GetOrderParameterSetting：" + ex.ToString());
                return null;
            }
        }

        public static int GetRyid(string openid)
        {
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@openid",openid)
            };
            string sql = "select ryid from idbind where openid=@openid";
            try
            {
                return Convert.ToInt32(SqlHelper.ExecuteScalar(conn, CommandType.Text, sql, sp));
            }
            catch (Exception ex)
            {

                ApplicationLog.Write("错误输出GetRyid：" + ex.ToString());
                return -1;
            }
        }

        public static bool SetOrderParameterSetting(SetOrderParameterSettingModel arguments)
        {
            List<SqlParameter> sp = new List<SqlParameter>();

            Type type = arguments.GetType();

            foreach (PropertyInfo property in type.GetProperties())
            {
                sp.Add(new SqlParameter($"@{property.Name}", property.GetValue(arguments)));
            }

            string sqlString = "";
            try
            {
                SqlHelper.ExecuteTransaction((transaction) =>
                {
                    sqlString = @"

                                insert into dbo.OrderList(OrderId,Ryid,TotalPrice,State,OrderTime,PayTime,payType,address1) 
                                values(@OrderId,@ryid,@money,2,GETDATE(),GETDATE(),'卡内余额',@TotalOrderId)

                                insert into dbo.OrderDetail(OrderId,OnSaleId,OrderNum,reservationdate,foodName,FoodPrice)
                                values(@OrderId,0,1,@date,@foodTime,@money)";
                    SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlString, sp.ToArray());

                });
                return true;
            }
            catch (Exception ex)
            {

                ApplicationLog.Write("错误输出SetOrderParameterSetting：" + ex.ToString());
                return false;
            }
        }


        //------包月订单下单支付，更新用户金额------
        public static int SetOrderParameterSettingPayAdd(int ryid, double amount,string oederIds)
        {
            SqlParameter[] sp = new SqlParameter[]
            {
                new SqlParameter("@ryid",ryid),
                new SqlParameter("@price",double.Parse(amount.ToString())),
                new SqlParameter("@oederIds",oederIds)
            };
            try
            {

                    string i = SqlHelper.ExecuteScalar(conn, CommandType.StoredProcedure, "OrderXFPayAdd", sp).ToString();
                    return Int32.Parse(i);

            }
#pragma warning disable CS0168 // 声明了变量“ex”，但从未使用过
            catch (Exception ex)
#pragma warning restore CS0168 // 声明了变量“ex”，但从未使用过
            {

                return -1;
            }



        }


        /// <summary>
        /// 通过编号查询余额
        /// </summary>
        /// <param name="user_phone"></param>
        /// <returns></returns>
        public static DataTable Select_CUST_ID(string rfkh)
        {
            DataTable dataTable = new DataTable();
            try
            {
                SqlParameter[] sp = new SqlParameter[] {
                new SqlParameter("@rfkh",rfkh)
            };
                string sql = "select bankcard,dz,r.bz,r.rfkh,rybh,r.ryid From ryxx r WITH(NOLOCK)  inner join lssj l WITH(NOLOCK)  on r.ryid=l.ryid where 1=1 and rfzt='32' and OrderNumber=@rfkh";
                return SqlHelper.ExecuteDataSet(conn, CommandType.Text, sql, sp).Tables[0];
            }
            catch (Exception ex)
            {
                ApplicationLog.Write("报错方法Select_CUST_ID：" + ex.Message);
                return dataTable;
            }
        }

    }
}
